| Conditions | 1 |
| Paths | 96 |
| Total Lines | 137 |
| Code Lines | 106 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | var TableInitSetColumnsHoverEffect = function(tContainer, tableId){ |
||
| 220 | function initInstance(){ |
||
| 221 | // Singleton |
||
| 222 | // Private methods and variables |
||
| 223 | var tail = []; |
||
| 224 | function LoadData(tableContainer, rq){ |
||
| 225 | TableHelperSetVisability(tableContainer, false); |
||
| 226 | if(window.XMLHttpRequest){ |
||
| 227 | var xmlhttp = new XMLHttpRequest();/* code for IE7+, Firefox, Chrome, Opera, Safari */ |
||
| 228 | }else{ |
||
| 229 | xmlhttp = new window.ActiveXObject("Microsoft.XMLHTTP");/*code for IE6, IE5 */ |
||
| 230 | } |
||
| 231 | for(var i = 0; i < tail.length; i++){ |
||
| 232 | var ex_xmlhttp = tail.shift(); |
||
| 233 | ex_xmlhttp.abort(); |
||
| 234 | } |
||
| 235 | xmlhttp.onreadystatechange = function(){ |
||
| 236 | if(xmlhttp.readyState === 4 && xmlhttp.status === 200){ |
||
| 237 | var d = JSON.parse(xmlhttp.responseText); |
||
| 238 | instance.rq = rq; |
||
| 239 | instance.Draw(tableContainer, d); |
||
| 240 | instance.LoadEndCalback(tableContainer); |
||
| 241 | TableHelperSetVisability(tableContainer, true); |
||
| 242 | } |
||
| 243 | }; |
||
| 244 | xmlhttp.open("GET", RequestToUrl(rq), true); |
||
| 245 | xmlhttp.send(); |
||
| 246 | tail.push(xmlhttp); //put at tail to can abort later any previous |
||
| 247 | } |
||
| 248 | function Init(tableId){ |
||
| 249 | var tContainer = document.getElementById(tableId); |
||
| 250 | if(iePrior(9)){ |
||
| 251 | TableInitSetColumnsHoverEffect(tContainer, tableId); |
||
| 252 | } |
||
| 253 | var tfoot = tContainer.getElementsByTagName("tfoot")[0]; |
||
| 254 | TableHelperProcessPaginationLinks(tfoot); |
||
| 255 | } |
||
| 256 | function Draw(tableContainer, d){ |
||
| 257 | TableHelperDraw_Section(tableContainer, d.body); |
||
| 258 | TableHelperDraw_Section(tableContainer, d.footer, "tfoot"); |
||
| 259 | if(this.rq !== null){ |
||
| 260 | var hover = document.getElementById(this.rq.tableId) |
||
| 261 | .getElementsByTagName("th")[this.rq.colNo].lang; |
||
| 262 | if(hover){ |
||
| 263 | instance.ColumnHover(tableContainer, this.rq.colNo); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | var BuildRequest = TableHelperBuildRequest; |
||
| 269 | var FilterGetTableId = TableHelperFilterGetTableId; |
||
| 270 | var getParent = TableHelperGetParent; |
||
| 271 | var iePrior = TableHelperIePrior; |
||
| 272 | var RequestToUrl = TableHelperRequestToUrl; |
||
| 273 | |||
| 274 | return { |
||
| 275 | rq: null, |
||
| 276 | strAsc: String.fromCharCode(9650), //▲ |
||
| 277 | strDesc: String.fromCharCode(9660),//▼ |
||
| 278 | ReloadData: function(tableId){ |
||
| 279 | var request = {}; |
||
| 280 | BuildRequest(request, tableId, this.strDesc); |
||
| 281 | LoadData(tableId, request); |
||
| 282 | }, |
||
| 283 | Filter: function(field){ |
||
| 284 | var crntTableId = FilterGetTableId(field); |
||
| 285 | if(crntTableId !== null){ |
||
| 286 | var request = {}; |
||
| 287 | var exRq = this.rq; |
||
| 288 | BuildRequest(request, crntTableId, this.strDesc); |
||
| 289 | if(exRq === null || |
||
| 290 | request.filter !== exRq.filter || |
||
| 291 | request.filterBy !== exRq.filterBy |
||
| 292 | ){ |
||
| 293 | LoadData(crntTableId, request); |
||
| 294 | } |
||
| 295 | } |
||
| 296 | }, |
||
| 297 | GoPage: function(lnk){ |
||
| 298 | var request = {}; |
||
| 299 | var table = getParent(lnk, "table"); |
||
| 300 | var crntTableId = table.getAttribute("id"); |
||
| 301 | BuildRequest(request, crntTableId, this.strDesc); |
||
| 302 | //check & serve pagination jump links |
||
| 303 | var jumpDir = lnk.innerHTML.trim().substr(0, 1); |
||
| 304 | if(jumpDir === "+" || jumpDir === "-"){ |
||
| 305 | var current = table.querySelector("tfoot .paging .a").innerHTML; |
||
| 306 | var jump = lnk.innerHTML.replace("K", "000").replace("M", "000000000"); |
||
| 307 | var jumpPage = (parseInt(current) + parseInt(jump)); |
||
| 308 | lnk.parentNode.setAttribute("data-page", jumpPage); |
||
| 309 | lnk.style.transform = "none"; |
||
| 310 | } |
||
| 311 | request.pageNo = lnk.parentNode.hasAttribute("data-page") ? |
||
| 312 | lnk.parentNode.getAttribute("data-page") : |
||
| 313 | lnk.innerHTML; |
||
| 314 | LoadData(crntTableId, request, this.strDesc); |
||
| 315 | return false; |
||
| 316 | }, |
||
| 317 | Export: function(lnk, eType){ |
||
| 318 | var request = {}; |
||
| 319 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
| 320 | BuildRequest(request, crntTableId, this.strDesc); |
||
| 321 | request.exportType = ["CSV", "Excel"].indexOf(eType) >= 0 ? |
||
| 322 | eType : |
||
| 323 | "csv"; |
||
| 324 | window.open(RequestToUrl(request)); |
||
| 325 | return false; |
||
| 326 | }, |
||
| 327 | Sort: function(colNo, lnk){ |
||
| 328 | var request = {}; |
||
| 329 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
| 330 | BuildRequest(request, crntTableId, this.strDesc); |
||
| 331 | if(Math.round(colNo) === request.colNo){ |
||
| 332 | request.colOrd = (request.colOrd === "asc" ? "desc" : "asc"); |
||
| 333 | }else{ |
||
| 334 | request.colNo = Math.round(colNo); |
||
| 335 | request.colOrd = "asc"; |
||
| 336 | } |
||
| 337 | LoadData(crntTableId, request); |
||
| 338 | /* Clear and add new sort arrow */ |
||
| 339 | var headSpans = getParent(lnk, "thead").getElementsByTagName("span"); |
||
| 340 | var length = headSpans.length; |
||
| 341 | for(var i = 0; i < length; i++){ |
||
| 342 | headSpans[i].innerHTML = ""; |
||
| 343 | } |
||
| 344 | lnk.getElementsByTagName("span")[0].innerHTML = (request.colOrd === "desc" ? this.strDesc : this.strAsc); |
||
| 345 | }, |
||
| 346 | |||
| 347 | ColumnHover: function(tableContainer, index){ |
||
| 348 | if(!iePrior(9)){ |
||
| 349 | TableHelperColumnHover.call(this, tableContainer, index); |
||
| 350 | } |
||
| 351 | }, |
||
| 352 | Draw: Draw, |
||
| 353 | LoadEndCalback: function(){},/*Allows override: function(tableId){if(tableId){...}}*/ |
||
| 354 | init: Init |
||
| 355 | }; |
||
| 356 | } |
||
| 357 | return { |
||
| 368 |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: